package sqlTestes.tTable;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import sql.ddl.PrimaryKey;
import sql.ddl.Schema;
import sql.ddl.Tabela;
import sql.ddl.coluna.Coluna;
import sql.ddl.coluna.ColunaComposta;
import sql.ddl.coluna.ListaColuna;
import sql.ddl.coluna.types.Int4;

public class TesteCriarTabelaAgua implements Especificacao {
	Tabela tabela = null;

	@Before
	public void up() {
		tabela = new Tabela("Agua");
	}

	@Test
	public void codigoSQLtabelaAgua() {
		Assert.assertEquals("CREATE TABLE Agua()", tabela.codigoSQL());
	}

	@Test
	public void codigoSQLtabelaAguaColunaInta() {
		tabela.addColuna(new Coluna("INTEGER", "a", false));
		Assert.assertEquals("CREATE TABLE Agua(a INTEGER NULL)", tabela
				.codigoSQL());
	}

	@Test
	public void codigoSQLtabelaAguaColunaFOREIGNKEYKEYInta() {
		Tabela a = new Tabela("A");
		a.setPrimaryKey(new PrimaryKey(new Coluna("SERIAL", "a")));
		ColunaComposta colunaAComposta;
		try {
			colunaAComposta = new ColunaComposta(a, "a", new Coluna("INTEGER",
					"a", "NULL"));
			tabela.addColuna(colunaAComposta);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Assert
				.assertEquals(
						"CREATE TABLE Agua(a INTEGER NULL,FOREIGN KEY(a)REFERENCES A(a))",
						tabela.codigoSQL());
	}

	@Test
	public void codigoSQLtabelaAguaColunaPRYMARYKEYInta() {
		PrimaryKey a = new PrimaryKey(new Coluna("INTEGER", "a", false));
		tabela.setPrimaryKey(a);
		Assert.assertEquals("CREATE TABLE Agua(a INTEGER NULL,PRIMARY KEY(a))",
				tabela.codigoSQL());
	}

	@Test
	public void nomeCompletoTabelaAguaSchemaAgua() {
		tabela.setSchema(new Schema("agua"));
		Assert.assertEquals("agua.Agua", tabela.getALLName());
	}

	@Test
	public void codigoSQLtabelaAguaSCHEMAaugaColunaFOREIGNKEYKEYInta() {
		PrimaryKey a = new PrimaryKey(new Coluna("INTEGER", "a", false));
		tabela.setPrimaryKey(a);
		tabela.setSchema(new Schema("agua"));
		Assert.assertEquals(
				"CREATE TABLE agua.Agua(a INTEGER NULL,PRIMARY KEY(a))", tabela
						.codigoSQL());
	}

	@Test
	public void testaQuandoTemApenasDuasColunasNaChavePrimaria() {
		ListaColuna cls = new ListaColuna();
		cls.add(new Int4("a", null));
		cls.add(new Int4("b", null));
		PrimaryKey a = new PrimaryKey(cls);
		tabela.setPrimaryKey(a);
		Assert.assertEquals(2, tabela.getALLColunas().size());
	}

	@Test
	public void testaQuandoTemApenas1ColunaNaChavePrimaria() {
		PrimaryKey a = new PrimaryKey(new Int4("a", null));
		tabela.setPrimaryKey(a);
		Assert.assertEquals(1, tabela.getALLColunas().size());
	}
}
